package com.d3works.apps.base.service.batch;

import com.d3works.apps.base.db.BaseBatch;
import com.d3works.apps.base.db.Batch;
import com.d3works.apps.base.db.repo.BaseBatchRepository;
import com.d3works.apps.base.exceptions.BaseExceptionMessage;
import com.d3works.apps.base.service.administration.AbstractBatchService;
import com.d3works.db.Model;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;

public class BaseBatchService extends AbstractBatchService {

  @Override
  protected Class<? extends Model> getModelClass() {
    return BaseBatch.class;
  }

  @Override
  public Batch run(Model batchModel) throws D3worksException {

    BaseBatch baseBatch = (BaseBatch) batchModel;

    switch (baseBatch.getActionSelect()) {
      case BaseBatchRepository.ACTION_SYNCHRONIZE_CALENDARS:
        return synchronizeCalendars(baseBatch);
      default:
        throw new D3worksException(
            TraceBackRepository.CATEGORY_INCONSISTENCY,
            I18n.get(BaseExceptionMessage.BASE_BATCH_1),
            baseBatch.getActionSelect(),
            baseBatch.getCode());
    }
  }

  public Batch synchronizeCalendars(BaseBatch baseBatch) {
    return Beans.get(BatchCalendarSynchronization.class).run(baseBatch);
  }
}
